package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/26.<br><br>
 *
 * 数组中的最长山脉
 */
public class Topic845 {

    public static void main(String[] args) {
        Topic845 instance = new Topic845();

        int[] array = new int[] {2,1,4,7,3,2,5};
        System.out.println(instance.longestMountain(array));

        array = new int[] {2,2,2};
        System.out.println(instance.longestMountain(array));

        array = new int[] {2,3};
        System.out.println(instance.longestMountain(array));

        array = new int[] {0,1,2,3,4,5,4,3,2,1,0};
        System.out.println(instance.longestMountain(array));

        array = new int[] {0,1,0};
        System.out.println(instance.longestMountain(array));

        array = new int[] {875,884,239,731,723,685};
        System.out.println(instance.longestMountain(array));
    }

    public int longestMountain(int[] arr) {
        int max = 0;
        int left;
        int right;

        int i = 0;
        while (i < arr.length) {
            while (i < arr.length - 1 && arr[i] >= arr[i + 1]) {
                i++;
            }

            left = i;
            while (i < arr.length - 1 && arr[i] < arr[i + 1]) {
                i++;
            }

            right = i;
            while (i < arr.length - 1 && arr[i] > arr[i + 1]) {
                i++;
            }

            if(i > right && right > left) {
                max = Math.max(max, i - left + 1);
            }
            else {
                i++;
            }
        }
        return max;
    }
}
